package com.estun.backroundManagementService.modules.workOrder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.factoryMes.request.base.BaseMesRequest;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.domain.factoryMes.FactoryMesResponse;
import com.estun.backroundManagementService.enums.WorkOrderStatusEnum;
import com.estun.backroundManagementService.enums.WorkOrderTypeEnum;
import com.estun.backroundManagementService.modules.workOrder.dto.WorkOrderEntity;
import com.estun.backroundManagementService.modules.workOrder.service.WorkOrderService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author yusiocng
 * @date 2023/11/30
 * @description 对外接口
 */
@RestController
@Api(tags = "ApiController", description = "对外开放接口")
@RequestMapping("/workOder")
@AllArgsConstructor
@Slf4j
public class WorkOrderController {

    private final WorkOrderService workOrderService;

    @PostMapping("/push")
    @Log(module = "工厂MES排程推送", type = LogConstants.INSERT, desc = "工厂MES排程推送")
    public FactoryMesResponse pushWorkOrder(@RequestBody BaseMesRequest<WorkOrderEntity> request) {
        log.info("【工单推送】 接收到参数:{}", request);
        return workOrderService.push(request);
    }


    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<WorkOrderEntity> pageRequest) {
        Page<WorkOrderEntity> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<WorkOrderEntity> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        queryWrapper.orderByDesc("schedule_start_time");
        Page<WorkOrderEntity> page1 = workOrderService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }

    @PostMapping("/pageReport")
    public BaseResponse pageReport(@RequestBody BasePageRequest<WorkOrderEntity> pageRequest) {
        Page<WorkOrderEntity> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<WorkOrderEntity> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        if (StringUtils.isNotBlank(pageRequest.getQuery().getBeginDate()) && StringUtils.isNotBlank(pageRequest.getQuery().getEndDate())) {
            queryWrapper.between("schedule_start_time", pageRequest.getQuery().getBeginDate(), pageRequest.getQuery().getEndDate());
        }
        queryWrapper.orderByDesc("schedule_start_time");
        Page<WorkOrderEntity> page1 = workOrderService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }

    @PostMapping("/create")
    @Log(module = "排程 自建排程", type = LogConstants.INSERT, desc = "排程 自建排程")
    public BaseResponse create(@RequestBody WorkOrderEntity request) {
        //此接口设置为产线mes自建工单 push默认工厂推送工单
        request.setType(WorkOrderTypeEnum.LINE_MES_NEW.getCode());
        return BaseResponse.success(workOrderService.save(request));
    }

    @PostMapping("/update")
    @Log(module = "排程 更新排程", type = LogConstants.UPDATE, desc = "排程 更新排程")
    public BaseResponse update(@RequestBody WorkOrderEntity request) {
//        return BaseResponse.success(workOrderService.updateById(request));
        String scheduleStatus = request.getScheduleStatus();
        Integer id = request.getId();
        WorkOrderEntity byId = workOrderService.getById(id);
        if (!byId.getScheduleStatus().equals(scheduleStatus)) {
            return workOrderService.updateScheduleStatus(byId, WorkOrderStatusEnum.getByCode(scheduleStatus));
        }
        return BaseResponse.error("排程状态与原状态相同,本次未更新");
    }

    @GetMapping("/updateScheduleQty")
    public BaseResponse updateScheduleQty() {
        return workOrderService.updateScheduleQty();
    }


    @GetMapping("/getLatestList")
    public BaseResponse getLatestList() {
        return BaseResponse.success(workOrderService.list(new QueryWrapper<WorkOrderEntity>()
                .eq("schedule_type", 1)
                .orderByDesc("schedule_start_time").last("LIMIT 10")));
    }

    /**
     * @return 获取当前生产中的工单
     */
    @GetMapping("/getInProcessSchedule")
    public BaseResponse getInProcessSchedule() {
        return BaseResponse.success(workOrderService.getInProcessSchedule());
    }

    /**
     * @return 获取当前生产中的工单对应的虚拟工单
     */
    @GetMapping("/getInProcessXpcSchedule")
    public BaseResponse getInProcessXpcSchedule() {
        return BaseResponse.success(workOrderService.getInProcessXpcSchedule());
    }
}
